#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int maxTurbulenceSize(vector<int>& arr) {
        int n = arr.size(), maxlen = 0;
        if (n == 1) return n;
        vector<int> dp(n);
        dp[0] = 1, dp[1] = 2;
        if (arr[1] == arr[0]) dp[1] = 1;
        maxlen = dp[1];
        for (int i = 2; i < n; i++)
        {
            if (arr[i] == arr[i - 1]) dp[i] = 1;
            else if ((long long)(arr[i] - arr[i - 1]) * (arr[i - 1] - arr[i - 2]) < 0) dp[i] = dp[i - 1] + 1;
            else dp[i] = 2;
            maxlen = max(maxlen, dp[i]);
        }
        return maxlen;
    }
};